﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;

namespace BootyShaker
{
    public class AppSettings 
    {
        public AppSettings()
        {

            try
            {
                settings = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (System.IO.IsolatedStorage.IsolatedStorageException e)
            {
                // handle exception
            }
        }

        // Our isolated storage settings
        IsolatedStorageSettings settings;

        // The isolated storage key names of our settings
        const string DampingRatioKeyName = "ShakerDampingRatio";
        const string FrequencyKeyName = "ShakerFrequency";
        const string GravityRatioKeyName = "ShakerGravityRatio";
      
        const string DragStrengthKeyName = "ShakerDragStrength";
        const string MergeOverlayKeyName = "MergeOverlay";
        // const string LinearDampingKeyName = "ShakerLinearDamping";
    
        public const double DampingRatioDefault = 0.7;
        public const double FrequencyDefault = 5;
        public const double GravityRatioDefault = 200;
       
        public const double DragStrengthDefault = 250;
        public const bool MergeOverlayDefault = false;
        //public const double LinearDampingDefault = 0.7;

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (T)settings[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            settings.Save();
        }

        ///// <summary>
        ///// Property to get and set a CheckBox Setting Key.
        ///// </summary>
        //public double LinearDampingSetting
        //{
        //    get
        //    {
        //        return GetValueOrDefault<double>(LinearDampingKeyName, LinearDampingDefault);
        //    }
        //    set
        //    {
        //        if (AddOrUpdateValue(LinearDampingKeyName, value))
        //        {
        //            Save();
        //        }
        //    }
        //}

        /// <summary>
        /// Property to get and set a CheckBox Setting Key.
        /// </summary>
        public double DragStrengthSetting
        {
            get
            {
                return GetValueOrDefault<double>(DragStrengthKeyName, DragStrengthDefault);
            }
            set
            {
                if (AddOrUpdateValue(DragStrengthKeyName, value))
                {
                    Save();
                }
            }
        }

       
       
        /// <summary>
        /// Property to get and set a CheckBox Setting Key.
        /// </summary>
        public double DampingRatioSetting
        {
            get
            {
                return GetValueOrDefault<double>(DampingRatioKeyName, DampingRatioDefault);
            }
            set
            {
                if (AddOrUpdateValue(DampingRatioKeyName, value))
                {
                    Save();
                }
            }
        }


        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public double FrequencySetting
        {
            get
            {
                return GetValueOrDefault<double>(FrequencyKeyName, FrequencyDefault);
            }
            set
            {
                if (AddOrUpdateValue(FrequencyKeyName, value))
                {
                    Save();
                }
            }
        }


        /// <summary>
        /// Property to get and set a RadioButton Setting Key.
        /// </summary>
        public double GravityRatioSetting
        {
            get
            {
                return GetValueOrDefault<double>(GravityRatioKeyName, GravityRatioDefault);
            }
            set
            {
                if (AddOrUpdateValue(GravityRatioKeyName, value))
                {
                    Save();
                }
            }
        }
        /// <summary>
        /// Property to get and set a RadioButton Setting Key.
        /// </summary>
        public bool MergeOverlaySetting
        {
            get
            {
                return GetValueOrDefault<bool>(MergeOverlayKeyName, MergeOverlayDefault);
            }
            set
            {
                if (AddOrUpdateValue(MergeOverlayKeyName, value))
                {
                    Save();
                }
            }
        }


    }
}
